home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: new-news.sprintlink.net!eskimo!news
- From: mag@eskimo.com (mAg)
- Subject: Re: Variant Records in C ... Is there a way ?
- X-Nntp-Posting-Host: tia1.eskimo.com
- Message-ID: <DotGHL.4wr@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: *.*
- X-Newsreader: WinVN 0.93.10
- References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt>
- Date: Mon, 25 Mar 1996 09:17:44 GMT
-
- In article <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> (Tue, 19 Mar 1996
- 17:12:18 +0000), l41471@alfa.ist.utl.pt says :
- >
- >
- > I would like to know if there is any way of implementing in C a
- >structure that if a determined argument (or info or depending on the
- >value of a particular field of a structure) could ignore or acknoledge a
- >certain field of the structure ...
- >
- > Example :
- > struct bla {
- > int field1;
- > int field2;
- > int field3;
- > }
- > And having a struct looking like this one, consider field3 if
- >field1=1, or ignore it (by not placing it in the data structure thus
- >saving memory ...)
- >
- > Any help would be highly apreciated ... (even if it's just to
- >say that there isn't a way ... it would make me stop searching for it ;))
- >
- >
- > Regards from :
- > Nuno Miguel Almeida Silva
- > l41471@alfa.ist.utl.pt
- > http://alfa.ist.utl.pt/~l41471
- >Instituto Superior Tecnico - Lisbon - Portugal
- >There's a major genocide going on in East Timor
-
- This is commonly done. Here is a crude example
- union _data_union_
- {
- char ch;
- int i;
- long l;
- float f;
- double d;
- void *p;
- / * and whatever else that you want */
- ...
- ...
- };
-
- typedef struct _variant_data_
- {
- int iDataType;
- union _data_union_ u;
- } VariantData;
-
- #define VARIANTDATAchar 1
- #define VARIANTDATAint 2
- #define VARIANTDATAlong 3
- #define VARIANTDATAfloat 4
- #define VARIANTDATAdouble 5
- #define VARIANTDATApointer 6
-
- And the Access Functions ...
-
-
- SetVariantInt(VariantData *pvd, int i)
- {
- pvd->u.i = i;
- pvd->iDataType = VARIANTDATAint;
- }
-
-
- And the function with a pointer to VariantData as an argument
-
- DoSomethingWithVariant(VariantData *pvd)
- {
- switch(pvd->iDataType)
- {
- case VARIANTDATAchar :
- /* do something with pvd->u.ch */
- ...
- ...
-
-
-
-
-
- }
-
- }
-
-
- hope that gives the idea...
- --
- /* --------------------------------------------------------
- MAG@ESKIMO.COM
- http://www.eskimo.com/~mag/index.html
- ***********************************************************
- To understand recursion one must first understand recursion
- ***********************************************************
- -------------------------------------------------------- */
-
-